#
# Makefile for modified UAE 68000 CPU core
#
# by James Hammons
# (C) 2011 Underground Software
#
# This makefile is released under the GPLv3 or later
#

# Cross compilation using MXE
#CROSS = i686-pc-mingw32-

CC      := $(CROSS)gcc
LD      := $(CROSS)gcc
AR      := $(CROSS)ar
HOSTCC  := gcc

ARFLAGS := -rs
GCC_DEPS = -MMD
INCS    := -I. -I./obj `$(CROSS)sdl-config --cflags`

OBJS = \
	obj/cpustbl.o \
	obj/cpudefs.o \
	obj/cpuemu.o \
	obj/cpuextra.o \
	obj/readcpu.o \
	obj/m68kinterface.o \
	obj/m68kdasm.o

# Targets for convenience sake, not "real" targets
.PHONY: clean

all: obj obj/libm68k.a
	@echo "Done!"

# Library rules (might not be cross-platform compatible)
obj/libm68k.a: $(OBJS)
	@$(AR) $(ARFLAGS) obj/libm68k.a $(OBJS)

obj:
	@mkdir ./obj

# Main source compilation (implicit rules)...

obj/%.o: %.c
	@echo -e "\033[01;33m***\033[00;32m Compiling $<...\033[00m"
	@$(CC) $(GCC_DEPS) $(CFLAGS) $(INCS) -c $< -o $@

obj/%.o: obj/%.c
	@echo -e "\033[01;33m***\033[00;32m Compiling $<...\033[00m"
	@$(CC) $(GCC_DEPS) $(CFLAGS) $(INCS) -c $< -o $@

# Generated code

obj/cpuemu.c: obj/gencpu
obj/cpustbl.c: obj/gencpu
	@echo -e "\033[01;33m***\033[00;32m Generating cpuemu.c...\033[00m"
	@cd obj && ./gencpu

obj/gencpu: obj/cpudefs.c
	@echo -e "\033[01;33m***\033[00;32m Generating gencpu...\033[00m"
	@$(HOSTCC) $(GCC_DEPS) $(CFLAGS) gencpu.c readcpu.c obj/cpudefs.c -o obj/gencpu -I. -I./obj

obj/cpudefs.c: obj/build68k
	@echo -e "\033[01;33m***\033[00;32m Generating cpudefs.c...\033[00m"
	@obj/build68k < table68k > obj/cpudefs.c

obj/build68k: build68k.c
	@echo -e "\033[01;33m***\033[00;32m Compiling build68k.c...\033[00m"
	@$(HOSTCC) $(GCC_DEPS) $(CFLAGS) build68k.c -o obj/build68k

clean:
	@echo -ne "\033[01;33m***\033[00;32m Cleaning out the garbage...\033[00m"
	@-rm -rf ./obj
	@echo "done!"

# Pull in dependencies autogenerated by gcc's -MMD switch
# The "-" in front is there just in case they haven't been created yet

-include obj/*.d
